package com.example.demohello.controller;


import com.example.demohello.entity.Student;

import com.example.demohello.service.StudentService;

import com.example.demohello.vo.ResultStudentVo;
import com.example.demohello.vo.StudentRequestBo;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class StudentController {

    @Autowired
    StudentService studentService;
    @RequestMapping("/selectStudent")
    @ResponseBody
    public  List<Student> selectStudent(StudentRequestBo requestBo){
       return studentService.selectForStudentList(requestBo);
    }

    @RequestMapping("/getOneStudent")
    @ResponseBody
    public  Student getOneStudent(Integer studentId){
        return studentService.selectOneStudent(studentId);
    }


    @RequestMapping("/deleteStudentById")
    @ResponseBody
    public  Integer deleteStudentById(Integer studentId){

        return studentService.deleteStudent(studentId);
    }


    @RequestMapping("/updateStudentById")
    @ResponseBody
    public  Integer updateStudentById(StudentRequestBo  studentRequestBo) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
        Timestamp startTime = new Timestamp(format.parse(studentRequestBo.getStudentBirthday()).getTime());
        Student student = new Student();
        student.setStudentName(studentRequestBo.getStudentName());
        student.setStudentId(studentRequestBo.getStudentId());
        student.setStudentSex(studentRequestBo.getStudentSex());
        student.setStudentBirthday(startTime);
        return studentService.updateStudent(student);
    }

    @RequestMapping("/addStudentBy")
    @ResponseBody
    public   Student addStudentBy(StudentRequestBo  studentRequestBo) throws ParseException {
//        String nowDate = new SimpleDateFormat("yyyyMMdd").format(studentRequestBo.getStudentBirthday());
//        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'");
        Timestamp startTime = new Timestamp(format.parse(studentRequestBo.getStudentBirthday()).getTime());
        Student student = new Student();
        student.setStudentName(studentRequestBo.getStudentName());
        student.setStudentId(studentRequestBo.getStudentId());
        student.setStudentSex(studentRequestBo.getStudentSex());
        student.setStudentBirthday(startTime);

        return studentService.addStudent(student);
    }


    @RequestMapping("/selectForStudentList")
    @ResponseBody
    public ResultStudentVo selectForStudentList(StudentRequestBo requestBo) throws ParseException {
        ResultStudentVo resultStudentVo  = new ResultStudentVo();
        System.out.println("  RequestBo   :   "+requestBo.toString());

        requestBo.setPageNum((requestBo.getPageNum()-1)*requestBo.getPageSize());
        resultStudentVo.setList(studentService.selectForStudentList(requestBo));
        resultStudentVo.setTotal(studentService.selectCountStudent(requestBo));
        return resultStudentVo;
    }

}
